<?php

include("netronix.php");

$fp = fsockopen("localhost", 5332, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
	rfid_start:
	fwrite($fp, C_Login_User(array(0x31, 0x32, 0x33, 0x34, 0x00)));
	usleep(60*1000);
	fwrite($fp, C_SetAutoReaderConfig(array(0x00, 0x00, 0x01, 0x01, 0x00)));
	usleep(60*1000);
	fwrite($fp, C_LoadKeyToSKB(array(0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00)));
	usleep(60*1000);
	fwrite($fp, C_TurnOnAntennaPower(array(0x01)));
	usleep(60*1000);
	fwrite($fp, C_Select(array(0x00)));
	usleep(60*1000);
	fwrite($fp, C_LoginWithSKB(array(0x00, 0xAA, 0x00)));
	usleep(60*1000);
	fwrite($fp, C_ReadBlock(array(0x00)));
	usleep(60*1000);
	//fwrite($fp, C_ReadBlock(array(0x01)));
	//usleep(50*1000);
	//fwrite($fp, C_ReadBlock(array(0x02)));
	//usleep(50*1000);
	//fwrite($fp, C_Reset());
	//usleep(50*1000);
	echo "Pobieram dane..\n";
	stream_set_timeout($fp, 1);
	//echo "Zapisuje do pliku..\n";
	//$rfid_data = fgets($fp, 250);
	//echo $rfid_data;
	$rfid_data = fgets($fp, 250);
	$hex = unpack('H*',$rfid_data);
	//echo $rfid_data;
	$hex_string = implode($hex, "");
	echo $hex_string;
	echo "\n\n";
	
	//$uid = substr($hex_string, 58, -42);
	//echo $uid;
	//$rfid_data_fp = fopen("srcp/rfid_data", "w");
	//fwrite($rfid_data_fp, $rfid_data);
	//fclose($rfid_data_fp);
	// sprawdzamy czy dane zostaly prawidlowo odczytane
	//if( strpos(file_get_contents("srcp/rfid_data"),"%") == false) {
		//fclose($fp);
        //goto rfid_start;
    //}
	sleep(1);
	//fwrite($fp, C_Login_User(array(0x31, 0x32, 0x33, 0x34, 0x00)));
	//usleep(50*1000);
	//fwrite($fp, C_SetAutoReaderConfig(array(0x01, 0x00, 0x01, 0x011, 0x01)));
	//goto rfid_start;
	fclose($fp);
}

?>